package com.pan.test.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.pan.test.config.OSSConfig;
import com.pan.test.service.OSSService;
import com.pan.test.utils.DateUtil;
import com.pan.test.entity.enums.DateTimePatternEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

@Service
public class OSSServiceImpl implements OSSService {

    private static final Logger logger = LoggerFactory.getLogger(OSSServiceImpl.class);

    @Autowired
    private OSS ossClient;

    @Autowired
    private OSSConfig ossConfig;

    @Override
    public String uploadFile(String userId, String fileId, MultipartFile file) {
        try {
            return uploadFile(userId, fileId, file.getInputStream(), file.getOriginalFilename());
        } catch (IOException e) {
            logger.error("文件上传失败", e);
            throw new RuntimeException("文件上传失败", e);
        }
    }

    @Override
    public String uploadFile(String userId, String fileId, InputStream inputStream, String fileName) {
        String objectName = String.format("%s/%s_%s", userId, fileId, fileName);
        
        try {
            logger.info("开始上传文件到OSS，objectName: {}", objectName);
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), objectName, inputStream);
            ossClient.putObject(putObjectRequest);
            logger.info("文件上传成功，objectName: {}", objectName);
            return objectName;
        } catch (Exception e) {
            logger.error("文件上传到OSS失败，objectName: {}, error: {}", objectName, e.getMessage(), e);
            throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
        }
    }

    @Override
    public void deleteFile(String objectName) {
        try {
            logger.info("开始删除OSS文件，objectName: {}", objectName);
            ossClient.deleteObject(ossConfig.getBucketName(), objectName);
            logger.info("OSS文件删除成功，objectName: {}", objectName);
        } catch (Exception e) {
            logger.error("删除OSS文件失败，objectName: {}, error: {}", objectName, e.getMessage(), e);
            throw new RuntimeException("删除文件失败: " + e.getMessage(), e);
        }
    }

    @Override
    public String getFileUrl(String objectName) {
        return String.format("https://%s.%s/%s", ossConfig.getBucketName(), ossConfig.getEndpoint(), objectName);
    }

    @Override
    public InputStream downloadFile(String objectName) {
        try {
            logger.info("开始下载OSS文件，objectName: {}", objectName);
            OSSObject ossObject = ossClient.getObject(ossConfig.getBucketName(), objectName);
            logger.info("OSS文件下载成功，objectName: {}", objectName);
            return ossObject.getObjectContent();
        } catch (Exception e) {
            logger.error("下载OSS文件失败，objectName: {}, error: {}", objectName, e.getMessage(), e);
            return null;
        }
    }
} 